package com.changgou.goods.controller;

import com.changgou.goods.pojo.Brand;
import com.changgou.goods.service.BrandService;
import com.github.pagehelper.PageInfo;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 1
 * @data 2022/1/8
 */
@RestController
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    /**
     * 查询所有品牌
     * @return
     */
    @GetMapping
    public Result<List<Brand>> findAll(){
        List<Brand> all = brandService.findAll();
        return new Result(true, StatusCode.OK,"查询所有品牌成功了！",all);
    }


    /**
     * 添加品牌
     * @param brand
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Brand brand){
        brandService.add(brand);
        return new Result(true, StatusCode.OK,"添加成功了！");
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<Brand> findById(@PathVariable(name = "id")Integer id){
        Brand brand = brandService.findById(id);
        return new Result(true,StatusCode.OK,"查询成功了",brand);
    }


    /**
     * 跟新数据
     * @param id 将要跟新的数据的id传递过来
     * @param brand 需要跟新的数据，根据id进行跟新
     * @return
     */
    @PutMapping("/{id}")
    public Result update(@PathVariable(name = "id") Integer id,@RequestBody Brand brand){
        brand.setId(id);
        brandService.update(brand);

        return new Result(true,StatusCode.OK,"修改成功了！");
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable(name = "id") Integer id){
        brandService.deleteById(id);

        return new Result(true,StatusCode.OK,"删除成功了！");
    }

    /**
     * 条件查询
     * @param brand
     * @return
     */
    @PostMapping("/search")
    public Result<List<Brand>> findList(@RequestBody Brand brand){
        List<Brand> brandList = brandService.findList(brand);
        return new Result<List<Brand>>(true,StatusCode.OK,"条件查询成功了！",brandList);
    }

    /**
     * 分页查询
     * @param page 当前端页码
     * @param size 每页显式的行
     * @return
     */
    @GetMapping("/search/{page}/{size}")
    public Result<PageInfo<Brand>> findByPage(@PathVariable(name = "page")Integer page,@PathVariable(name = "size") Integer size){
        PageInfo<Brand> brandList = brandService.findByPage(page,size);

        return new Result<PageInfo<Brand>>(true,StatusCode.OK,"分页查询成功！",brandList);
    }

    @PostMapping("/search/{page}/{size}")
    public Result<PageInfo<Brand>> findByPage(@PathVariable(name = "page") Integer page,@PathVariable(name = "size")Integer size,@RequestBody Brand brand){
        PageInfo<Brand> brandPageInfo = brandService.findByPage(page,size,brand);
        //int a = 1/0;
        return new Result<PageInfo<Brand>>(true,StatusCode.OK,"条件分页查询成功！",brandPageInfo);
    }
    //----------------------------------分割线----------------------------------分割线----------------------------------
    @GetMapping("/category/{id}")
    public Result<List<Brand>> findBrandList(@PathVariable(name = "id") Integer id){
        List<Brand> brandList = brandService.findBrandList(id);

        return new Result<List<Brand>>(true,StatusCode.OK,"品牌列表查询成功了！",brandList);
    }

    @GetMapping("/test")
    public Result<List<Brand>> findAllBrand(){

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        List<Brand> all = brandService.findAll();

        return new Result<List<Brand>>(true,StatusCode.OK,"查询成功",all);
    }
}
